﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ATM_Software
{
    public class CustomerDAL
    {
        StreamWriter writer;
        StreamReader reader;
        const string ACCOUNTS_FILE = "..\\..\\account.txt";
        const string LOGINS_FILE = "..\\..\\login.txt";
       
        public int CreateAccount(CustomerData customer)
        {
            if (File.Exists(ACCOUNTS_FILE))
            {
                using (reader = new StreamReader(ACCOUNTS_FILE))
                {
                    string lastLine = null;
                    string tempLine = null;                
                    while((tempLine=reader.ReadLine())!=null)
                    {
                        lastLine = tempLine;
                    }
                    if (lastLine != null) // to assign new user id and account number
                    {
                        string[] tokens = null;
                        tokens = lastLine.Split(';');
                        customer.UserId = int.Parse(tokens[0]) + 1;
                        customer.AccountId = int.Parse(tokens[1]) + 1;
                    }
                    else // if account.txt is empty
                    {
                        customer.UserId = 101;
                        customer.AccountId = 1;
                    }                    
                }
               
            }
            else
            {
                customer.UserId = 101;
                customer.AccountId = 1;
            }

            using (writer = new StreamWriter(ACCOUNTS_FILE ,true))
            {
                writer.WriteLine(customer.ToAccountString());             
            }

            using (writer = new StreamWriter(LOGINS_FILE, true))
            {
                writer.WriteLine(customer.ToLoginString());
            }

            return customer.AccountId;
        }
        public List<CustomerData> Search(CustomerData customer)
        {
            List<CustomerData> customersList = new List<CustomerData>();
            if (File.Exists(ACCOUNTS_FILE) && File.Exists(LOGINS_FILE))
            {                
                string[] tokens = null;
                string[] tokens1 = null;
                int counter = 0;
                string[] accounts = File.ReadAllLines(ACCOUNTS_FILE);                
                string[] logins = File.ReadAllLines(LOGINS_FILE);                

                foreach (string record in accounts)
                {
                  
                    tokens = record.Split(';');
                    tokens1 = logins[counter].Split(';');
                    CustomerData tempCustomer = new CustomerData(customer);
                    if ((customer.AccountId == 0 || customer.AccountId.ToString().Equals(tokens[1])) &&
                        (customer.UserId == 0 || customer.UserId.ToString().Equals(tokens[0])) &&
                        (customer.Name.Equals("") || customer.Name.Equals(tokens[2])) &&
                        (customer.Type.Equals("") || customer.Type.Equals(tokens[3])) &&
                        (customer.Balance == null || customer.Balance.ToString().Equals(tokens[4])) &&
                        (customer.Status.Equals("") || customer.Status.Equals(tokens[5]))
                        && (customer.Login.Equals("") || customer.Login.Equals(tokens1[1]))&&
                        customer.isDefaultObject()==false)
                    {                                                
                        tempCustomer.UserId = int.Parse(tokens[0]);
                        tempCustomer.AccountId = int.Parse(tokens[1]);
                        tempCustomer.Name = tokens[2];
                        tempCustomer.Type = tokens[3];
                        tempCustomer.Balance = int.Parse(tokens[4]);
                        tempCustomer.Status = tokens[5];
                        //foreach (string loginInfo in logins)
                      //  {
                            /* if first index that is userid in login.txt is equal to current tempCustomer's
                                * userid then update tempCustomers object's login info also*/
                            
                          //  if (int.Parse(tokens[0]) == tempCustomer.UserId)
                        //    {
                                tempCustomer.Login = tokens1[1];
                                tempCustomer.PinCode = int.Parse(tokens1[2]);
                         //   }
                       // }
                        tempCustomer.EncryptDecrypt(); // to decrypt the stored login info
                        customersList.Add(tempCustomer);                        
                    }
                    counter++;
                }                                
                return customersList;
            }
            return null;
        }
        public bool DeleteAccount( CustomerData customer)
        {
            string accounts = null;
            string logins = null;
            string transactions = null;
    
            using (reader = new StreamReader(ACCOUNTS_FILE))
            {
                accounts = reader.ReadToEnd();                
                accounts = accounts.Replace(customer.ToAccountString() + "\r\n","");
                
            }

            using (reader = new StreamReader(LOGINS_FILE))
            {
                logins = reader.ReadToEnd();
                customer.EncryptDecrypt();
                logins =  logins.Replace(customer.ToLoginString() + "\r\n","");                
            }

            string[] records = File.ReadAllLines("..\\..\\transactions.txt");
            foreach (string item in records)
            {
                if (int.Parse(item.Split(';')[0]) != customer.AccountId)
                {
                    transactions += item + "\r\n";
                }
                
            }            

            using (writer = new StreamWriter(ACCOUNTS_FILE))
            {
                writer.Write(accounts);
            }

            using (writer = new StreamWriter(LOGINS_FILE))
            {
                writer.Write(logins);
            }

            using (writer = new StreamWriter("..\\..\\transactions.txt"))
            {
                writer.Write(transactions);
            }
            return true;
        }
        public bool UpdateAccount(CustomerData customer)
        {
            string accounts = "";
            string logins = "";
            using (reader = new StreamReader(ACCOUNTS_FILE))
            {
                string temp = null;
                while ((temp = reader.ReadLine())!=null)
                {
                    if (int.Parse(temp.Split(';')[0]) == customer.UserId)
                    {
                        accounts += customer.ToAccountString() + "\r\n";
                    }
                    else
                    {
                        accounts += temp + "\r\n";              
                    }                    
                }                
            }
            using (reader = new StreamReader(LOGINS_FILE))
            {
                string temp = null;
                while ((temp = reader.ReadLine()) != null)
                {
                    if (int.Parse(temp.Split(';')[0]) == customer.UserId)
                    {
                        customer.EncryptDecrypt();
                        logins += customer.ToLoginString() + "\r\n";
                    }
                    else
                    {
                        logins += temp + "\r\n";
                    }
                }
            }
            using (writer = new StreamWriter(ACCOUNTS_FILE))
            {                
                writer.Write(accounts);
            }
            using (writer = new StreamWriter(LOGINS_FILE))
            {                
                writer.Write(logins);
            }
            return true;
        }

        public bool Login(ref CustomerData customer)
        {
            customer.EncryptDecrypt();
            List<CustomerData> customerList =  Search(customer);
            if (customerList.Count != 0)
            {
                customer = customerList[0];
                return true;
            }                        
            return false;
        }
        public List<CustomerData> GetAllAcounts()
        {
            List<CustomerData> allAccounts = new List<CustomerData>();
            CustomerData tempCustomer;
            string[] tokens = null;
            if (File.Exists(ACCOUNTS_FILE))
            {
                string[] allRecords = File.ReadAllLines(ACCOUNTS_FILE);
                foreach (string record in allRecords)
                {
                    tempCustomer = new CustomerData();
                    tokens = record.Split(';');
                    tempCustomer.UserId = int.Parse(tokens[0]);
                    tempCustomer.AccountId = int.Parse(tokens[1]);
                    tempCustomer.Name = tokens[2];
                    tempCustomer.Type = tokens[3];
                    tempCustomer.Balance = int.Parse(tokens[4]);
                    tempCustomer.Status = tokens[5];
                    allAccounts.Add(tempCustomer);
                }
                return allAccounts;
            }
            return null;
        }
        
    }
}

